//
//  XJCommentMSgViewController.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/7/2.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJCommentMSgViewController.h"

#import "XJCommentMsgModel.h"
#import "XJCommentMsgCell.h"


@interface XJCommentMSgViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation XJCommentMSgViewController

#pragma mark - 一 lazy
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        
        NSString *classStr = NSStringFromClass([XJCommentMsgCell class]);
        [_tableView registerClass:[XJCommentMsgCell class] forCellReuseIdentifier:classStr];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


#pragma mark - 二 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"回复我的";
    
    
    self.tableView.frame = CGRectMake(0, 0, KWidth, KHeight - kStatusBarAndNavigationBarHeight);
    [self.view addSubview:self.tableView];
    
    [self requeatData];
}

#pragma  mark - 三  UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *classStr = NSStringFromClass([XJCommentMsgCell class]);
    
    XJCommentMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:classStr];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.model = self.dataArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    XJCommentMsgModel *model = self.dataArray[indexPath.row];
    
    return model.cellHeight;
}


#pragma mark - 四 网络请求

- (void)requeatData {
    [XJHttpTool requestInvitationReplyMeWithPage:@"1" successH:^(id responseObjc) {
        
      
        NSString *code = [NSString stringWithFormat:@"%@",responseObjc[@"code"]];
        if ([code isEqualToString:@"0"]) {
            NSArray * list = responseObjc[@"data"][@"list"];
            for (NSDictionary *dic in list) {
                XJCommentMsgModel *model = [XJCommentMsgModel mj_objectWithKeyValues:dic];
                
                [self.dataArray addObject:model];
            }
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    } failH:^(NSError *error) {
        [JRToast showWithText:[error localizedDescription]];
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 服务器返回数据
/**
 XJCommentMSgViewController.m:32    {
 "data" : {
 "list" : [
 {
 "reply_time" : 1528461377867,
 "publish_nickname" : "都纷纷",
 "publish_text" : "可口可乐公司在这方面也",
 "reply_text" : "办理",
 "reply_level" : 2,
 "type" : 0,
 "invitation_id" : 48,
 "target_id" : 503,
 "is_delete" : 1,
 "invitation_type" : "ymt",
 "root_id" : 503,
 "reply_customer_id" : 13,
 "reply_avatar" : "http:\/\/static.zertone1.com\/upload\/avatar\/2018_03_23\/acf3afd39f2d4f44b7f00909e70d5297.jpg",
 "reply_nickname" : "H1",
 "publish_customer_id" : 7
 },
 {
 "reply_time" : 1528461089301,
 "publish_nickname" : "都纷纷",
 "publish_text" : "可口可乐公司在这方面也",
 "reply_text" : "1444",
 "reply_level" : 2,
 "type" : 0,
 "invitation_id" : 48,
 "target_id" : 503,
 "is_delete" : 1,
 "invitation_type" : "ymt",
 "root_id" : 503,
 "reply_customer_id" : 13,
 "reply_avatar" : "http:\/\/static.zertone1.com\/upload\/avatar\/2018_03_23\/acf3afd39f2d4f44b7f00909e70d5297.jpg",
 "reply_nickname" : "H1",
 "publish_customer_id" : 7
 },
 {
 "reply_time" : 1528459497848,
 "publish_nickname" : "都纷纷",
 "publish_text" : "这么多年都没看到过这么的好电影推荐一首好歌推荐可口可乐了可口可乐。滚滚滚滚滚滚",
 "reply_text" : "考虑考虑",
 "reply_level" : 2,
 "type" : 0,
 "invitation_id" : 48,
 "target_id" : 486,
 "is_delete" : 1,
 "invitation_type" : "ymt",
 "root_id" : 486,
 "reply_customer_id" : 13,
 "reply_avatar" : "http:\/\/static.zertone1.com\/upload\/avatar\/2018_03_23\/acf3afd39f2d4f44b7f00909e70d5297.jpg",
 "reply_nickname" : "H1",
 "publish_customer_id" : 7
 }
 ]
 },
 "code" : 0
 }

 */

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
